package com.wgtgt.swing_paint;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import cn.hutool.core.img.FontUtil;

public class MyFrame extends JFrame {
	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 5456284374661877331L;

	@Override
	public void paint(Graphics g) {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] availableFontFamilyNames = ge.getAvailableFontFamilyNames();
		Graphics2D gg = (Graphics2D) g;
		gg.setColor(new Color(0, 0, 255));
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		Font ff = null;
		Dimension dimension = null;
		FontMetrics metrics = null;
		gg.setColor(new Color(2, 1, 15));
		String str = "demo";
		gg.setFont(new Font("宋体", Font.PLAIN + Font.BOLD, 44));
		gg.drawString(str, 199, 121);
		gg.setFont(ff = new Font("宋体", Font.PLAIN + Font.BOLD, 40));
		gg.setColor(new Color(230, 2, 2));
		String title = "test";
		metrics = gg.getFontMetrics(ff);
		dimension = FontUtil.getDimension(metrics, title);
		gg.drawString(title, Math.round((800 - dimension.getWidth()) / 2.0d) + 0, 230);
		gg.setFont(new Font("宋体", Font.PLAIN + Font.BOLD, 48));
		gg.setColor(new Color(2, 1, 200));
		gg.drawString("", 359, 285);
		gg.setColor(new Color(1, 200, 2));
		gg.setFont(ff = new Font("宋体", Font.PLAIN + Font.BOLD, 24));
		title = "hello";
		metrics = gg.getFontMetrics(ff);
		dimension = FontUtil.getDimension(metrics, title);
		gg.drawString(title, Math.round((800.0 - dimension.getWidth()) / 2.0d), 325);
		gg.dispose();
	}

	private Color ccc(int index) {
		List<Color> list = new ArrayList<Color>();
		int[] cc = new int[3];
		cc = new int[] { 255, 0, 0 };
		int flag = 1;
		int addNum = 127;
		int size = 12;
		int indexNum = 2;
		for (int i = 0; i < size; i++) {
			if (i % 4 == 0 || i % 4 == 1) {
				flag = 1;
			} else {
				flag = -1;
			}
			if (i % 2 == 0) {
				indexNum--;
			}
			if (indexNum < 0) {
				indexNum = 2;
			}
			cc[indexNum] += flag * addNum;
			if (cc[indexNum] == 128 || cc[indexNum] == 1) {
				cc[indexNum]--;
			}
			if (cc[indexNum] == 254) {
				cc[indexNum]++;
			}
			Color vv = new Color(cc[0], cc[1], cc[2]);
			list.add(vv);
		}
		return list.get(index % list.size());
	}
}
